home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / FCOPY.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  1KB  |  56 lines

  1. /* 
  2.  * FCOPY.C - copy one file to another.  Returns the (positive)
  3.  *           number of bytes copied, or -1 if an error occurred.
  4.  * by: Bob Jarvis
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. #define BUFFER_SIZE 1024
  11.  
  12. long fcopy(char *dest, char *source)
  13. {
  14.         FILE *d, *s;
  15.         char *buffer;
  16.         size_t incount;
  17.         long totcount = 0L;
  18.  
  19.         s = fopen(source, "rb");
  20.         if(s == NULL)
  21.                 return -1L;
  22.  
  23.         d = fopen(dest, "wb");
  24.         if(d == NULL)
  25.         {
  26.                 fclose(s);
  27.                 return -1L;
  28.         }
  29.  
  30.         buffer = malloc(BUFFER_SIZE);
  31.         if(buffer == NULL)
  32.         {
  33.                 fclose(s);
  34.                 fclose(d);
  35.                 return -1L;
  36.         }
  37.  
  38.         incount = fread(buffer, sizeof(char), BUFFER_SIZE, s);
  39.  
  40.         while(!feof(s))
  41.         {
  42.                 totcount += (long)incount;
  43.                 fwrite(buffer, sizeof(char), incount, d);
  44.                 incount = fread(buffer, sizeof(char), BUFFER_SIZE, s);
  45.         }
  46.  
  47.         totcount += (long)incount;
  48.         fwrite(buffer, sizeof(char), incount, d);
  49.  
  50.         free(buffer);
  51.         fclose(s);
  52.         fclose(d);
  53.  
  54.         return totcount;
  55. }
  56.